<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />

<html>
<head>
<title>_保存库存异动</title>


<script type="text/javascript">
	$(function(){
		$("#saveForm").validate();
		if('${result}'!=''&&'${result}'!='success'){
			if("${errorMsg}") {
				
			}
			jAlert('${errorMsg}');
		}
	})
	var customerList = new Array();
	<c:forEach items="${customerList}" var="customer">
	customerList.push({
		"customerCode" : "${customer.customerCode}",
		"customerName" : "${customer.customerName}"
	});
	</c:forEach>

	var warehouseList = new Array();
	<c:forEach items="${warehouseList}" var="warehouse">
	warehouseList.push({
		"warehouseCode" : "${warehouse.groupCode}",
		"warehouseName" : "${warehouse.groupName}"
	});
	</c:forEach>
	
	
     
	var index = 0;
	<c:if test="${!empty scbiList}">
		index = ${scbiList.size()};
	</c:if>
	function addRow(event) {
		var tr = '<tr id="tr_'+index+'" >';
		tr += '<td>';
		tr += '<select style="width:90%;" class="required form-control span3"   name="scbiList['+index+ '].inventoryType"  onKeydown="if(event.keyCode==13){addRow()}">';
			tr += '<option></option>';
			tr += '<option value="error">完好件</option>';
			tr += '<option value="noterror">破损件</option>';
		tr += '</select>';
		tr += '</td>';
		tr += '<td>';
		tr += '<input style="width:90%;" class="required form-control span3"   name="scbiList['+ index+ '].sku"  onKeydown="if(event.keyCode==13){addRow()}"/>';
		tr += '</td>';
		tr += '<td>';
		tr += '<input style="width:90%;" class="required form-control span3"  id="quantity_'+index+'" name="scbiList['+ index+ '].quantity"  onKeydown="if(event.keyCode==13){addRow()}"/>';
		tr += '</td>';
		tr += '<td>';
		tr += '<button class="btn btn-danger" onclick="delRow(' + index	+ ')" type="button">删除</button>';
		tr += '</td>';
		tr += "</tr>";
		$("#tbodyId").append(tr);
		
		$("select").not(".oldSelectStyle").attr('data-placeholder', '请选择...');
		$("select").not(".oldSelectStyle").chosen({
			search_contains : true,//搜索所有字符
			allow_single_deselect : true, //显示取消选项按钮
			width : "100%",
		}).change(function() {
			try {
				if ($(this).valid()) {
					$("span[for=" + $(this).attr("id") + "]",
							$(this).parent()).remove();
				}
			} catch (e) {
			}
		});
		index++;
	}
	function delRow(pInex) {
		$("#tr_" + pInex).remove();
	}
	function saveAll() {
		var openQm = $("#customerCode").find("option:selected").attr("openqm");
		if(openQm != "1"){
			jAlert("奇门商家才能发起库存异动！");			
			return;
		}
		if($("#tbodyId tr").length==0)
		{
			jAlert("至少存在一条明细数据");
			return;
		}
		for(var i = 0 ;i < index ;i++){
			var q = $('#quantity_'+i);
			if(q.length > 0 ){
				var tt=/^-?\d+$/g;
				var result = (q.val()!="" && !tt.test(q.val()+""));
				if(result ){
				    jAlert("数量必须是整数！");
				    return ;  
				}
			}
		}
		$("#saveForm").submit();
	}
	

	
	function back(){
		window.location.href = '${ctx}/stockChangeBill/v2_stockChangePage';
	}
</script>
</head>

<body>
	<div id="fullbg"></div>
	<div id="dialog">
		<div>
			<img src="${ctx}/static/022.gif"></img>
		</div>
	</div>
	<section class="content">
		<form class="form-search" id="saveForm" method="post"
			action="${ctx}/stockChangeBill/v2_saveStockChange"
			style="margin-top: 10px;">
			<input type="hidden" value="${scb.id}" id="id" name="scb.id">
			<div class="row">
				<div class="col-md-12">

					<!-- Primary box -->
					<div class="box box-solid box-primary">
						<div class="box-header">

							<h3 class="box-title">异动信息</h3>
							<div class="box-tools pull-right">
								<button class="btn btn-primary btn-sm" data-widget="collapse"
									type="button">
									<i class="fa fa-minus"></i>
								</button>
							</div>
						</div>
						<div class="box-body">

							<div class="form-group row">
								<div class="col-sm-3">
									<div class="input-group col-sm-12">
										<span class="input-group-text"> 商家名称： </span> <select
											class="form-control required" id="customerCode"
											name="scb.customerCode"
											onchange="ajaxAreaGroup(this,'warehouseCode')">
											<option value=""></option>
											<c:forEach items="${customerList}" var="customer">
												<option value="${customer.customerCode}"
													openqm="${customer.openQm}"
													<c:if test="${scb.customerCode eq customer.customerCode}">selected=selected</c:if>>${customer.customerName}</option>
											</c:forEach>
										</select>
									</div>
								</div>

								<div class="col-sm-3">
									<div class="input-group col-sm-12">
										<span class="input-group-text"> 仓库名称： </span> <select
											class="form-control span3 required" id="warehouseCode"
											name="scb.warehouseCode">
											<option value=""></option>
											<c:forEach items="${warehouseList}" var="warehouse">
												<option value="${warehouse.groupCode}"
													<c:if test="${scb.warehouseCode eq warehouse.groupCode }">selected=selected</c:if>>${warehouse.groupName}</option>
											</c:forEach>
										</select>
									</div>
								</div>

								<div class="col-sm-3">
									<div class="input-group col-sm-12">
										<span class="input-group-text"> 单据类型： </span> <select
											class="form-control span3 required" id="warehouseCode"
											name="scb.orderType">
											<option value=""></option>
											<option value="INSTORAGE" <c:if test="${scb.orderType eq 'INSTORAGE' }">selected=selected</c:if>>入库单</option>
											<option value="ORDER" <c:if test="${scb.orderType eq 'ORDER' }">selected=selected</c:if>>销售订单</option>
										</select>
									</div>
								</div>

								<div class="col-sm-3">
									<div class="input-group col-sm-12">
										<span class="input-group-text"> 相关单号： </span>
										 <input type="text" name="scb.orderCode" id="orderCode" value="${scb.orderCode}" class="required form-control span3">
									</div>
								</div>
							</div>
							<div class="form-group row">
								<div class="col-sm-6">
									<div class="input-group col-sm-12">
										<span class="input-group-text"> 备注： </span>
										<input	class="form-control" type="text" id="remark" value="${scb.remark}" name="scb.remark" />
									</div>
								</div>
							</div>
						</div>

					</div>
					<!-- /.box-body -->

				</div>
				<!-- /.col -->
			</div>
			<div class="row">
				<div class="col-md-12">

					<!-- Primary box -->
					<div class="box box-solid box-primary">

						<div class="box-header">
							<!-- 						<i class="fa fa-search"></i> -->
							<h3 class="box-title"></h3>
						</div>

						<div class="box-body">
							<p align="left">
								<a data-toggle="modal" role="button" class="btn btn-primary"
									onclick="addRow();">添加行(输入框内回车添加行)</a><br />
							</p>
							<table id="table_occupiedAreaConfig"
								class="table table-bordered table-striped">
								<thead>
									<tr>
										<th style="width: 200px;">是否破损</th>
										<th style="width: 200px;">SKU</th>
										<th style="width: 80px;">异动数量</th>
										<th style="width: 80px;">操作</th>
									</tr>
								</thead>
								<tbody id="tbodyId">
									<c:forEach items="${scbiList}" var="scbi" varStatus="vs">
										<tr id="tr_${vs.index}" >
											<td>
												<select style="width:90%;" class="required form-control span3"   name="scbiList[${vs.index}].inventoryType"  onKeydown="if(event.keyCode==13){addRow()}">
													<option></option>
													<option value="error"  <c:if test="${scbi.inventoryType eq 'error' }">selected=selected</c:if>>完好件</option>
													<option value="noterror" <c:if test="${scbi.inventoryType eq 'noterror' }">selected=selected</c:if>>破损件</option>
												</select>
											</td>
											<td>
												<input style="width:90%;" class="required form-control span3"   name="scbiList[${vs.index}].sku"  value="${scbi.sku}" onKeydown="if(event.keyCode==13){addRow()}"/>
											</td>
											<td>
												<input style="width:90%;" class="required form-control span3" id="quantity_${vs.index}" name="scbiList[${vs.index}].quantity" value="${scbi.quantity}"   onKeydown="if(event.keyCode==13){addRow()}"/>
											</td>
											<td>
												<button class="btn btn-danger" onclick="delRow(${vs.index})" type="button">删除</button>
											</td>
										</tr>
									</c:forEach>
								</tbody>

							</table>

						</div>
					</div>
					<p align="right">
						<a data-toggle="modal" role="button" class="btn btn-primary"
							onclick="saveAll();">保存所有数据</a>
						<a data-toggle="modal" role="button" class="btn "
							onclick="back();">返回</a>
					</p>
				</div>
			</div>
		</form>
	</section>
</body>
</html>
